package com.zc.manager.device.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.common.constant.HttpStatus;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.DateUtils;
import com.zc.manager.device.domain.IotDeviceTags;
import com.zc.manager.device.mapper.IotDeviceTagsMapper;
import com.zc.manager.device.service.IIotDeviceTagsService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.UUID;

/**
 * 设备标签Service业务层处理
 *
 * @author xiepufeng
 * @date 2022-08-30
 */
@Service
public class IotDeviceTagsServiceImpl extends ServiceImpl<IotDeviceTagsMapper, IotDeviceTags> implements IIotDeviceTagsService {
    @Resource
    private IotDeviceTagsMapper iotDeviceTagsMapper;

    /**
     * 查询设备标签
     *
     * @param id 设备标签主键
     * @return 设备标签
     */
    @Override
    public IotDeviceTags selectIotDeviceTagsById(Long id) {
        return iotDeviceTagsMapper.selectIotDeviceTagsById(id);
    }

    /**
     * 查询设备标签
     *
     * @param key 设备标签key
     * @return 设备标签
     */
    @Override
    public IotDeviceTags selectIotDeviceTagsByKey(String key) {
        return iotDeviceTagsMapper.selectIotDeviceTagsByKey(key);
    }

    /**
     * 查询设备标签
     *
     * @param deviceId   设备id
     * @param tagKeyList 设备标签key
     * @return 设备标签
     */
    @Override
    public List<IotDeviceTags> selectIotDeviceTagsByKeysAndDeviceId(String deviceId, List<String> tagKeyList) {
        return iotDeviceTagsMapper.selectIotDeviceTagsByKeysAndDeviceId(deviceId, tagKeyList);
    }

    /**
     * 查询设备标签列表
     *
     * @param iotDeviceTags 设备标签
     * @return 设备标签
     */
    @Override
    public List<IotDeviceTags> selectIotDeviceTagsList(IotDeviceTags iotDeviceTags) {
        return iotDeviceTagsMapper.selectIotDeviceTagsList(iotDeviceTags);
    }

    /**
     * 新增设备标签
     *
     * @param iotDeviceTags 设备标签
     * @return 结果
     */
    @Override
    public int insertIotDeviceTags(IotDeviceTags iotDeviceTags) {
        iotDeviceTags.setCreateTime(DateUtils.getNowDate());
        return iotDeviceTagsMapper.insertIotDeviceTags(iotDeviceTags);
    }

    /**
     * 修改设备标签
     *
     * @param iotDeviceTags 设备标签
     * @return 结果
     */
    @Override
    public int updateIotDeviceTags(IotDeviceTags iotDeviceTags) {
        iotDeviceTags.setUpdateTime(DateUtils.getNowDate());
        return iotDeviceTagsMapper.updateIotDeviceTags(iotDeviceTags);
    }

    /**
     * 批量删除设备标签
     *
     * @param ids 需要删除的设备标签主键
     * @return 结果
     */
    @Override
    public int deleteIotDeviceTagsByIds(Long[] ids) {
        return iotDeviceTagsMapper.deleteIotDeviceTagsByIds(ids);
    }

    /**
     * 删除设备标签信息
     *
     * @param id 设备标签主键
     * @return 结果
     */
    @Override
    public int deleteIotDeviceTagsById(Long id) {
        return iotDeviceTagsMapper.deleteIotDeviceTagsById(id);
    }

    /**
     * 批量新增设备标签
     *
     * @param iotDeviceTagsList 新增或修改的数据
     * @return 结果
     */
    @Override
    public int insertListIotDeviceTags(List<IotDeviceTags> iotDeviceTagsList) {
        for (IotDeviceTags iotDeviceTags : iotDeviceTagsList) {
            if (iotDeviceTags.getId() == null && iotDeviceTags.getKey() != null && iotDeviceTags.getName() != null && iotDeviceTags.getValue() != null) {
                List<IotDeviceTags> deviceTags = iotDeviceTagsMapper.selectIotDeviceTagsByDeviceIdAndKey(iotDeviceTags.getDeviceId(), iotDeviceTags.getKey());
                if (deviceTags.size() != 0) {
                    throw new ServiceException("标签KEY值不能重复", HttpStatus.BAD_REQUEST);
                }
                Integer uuid = UUID.randomUUID().toString().replaceAll("-", "").hashCode();
                uuid = uuid < 0 ? -uuid : uuid;
                iotDeviceTags.setId(uuid.longValue());
                iotDeviceTags.setCreateTime(new Date());
                iotDeviceTagsMapper.insertIotDeviceTags(iotDeviceTags);
            } else if (iotDeviceTags.getId() != null && iotDeviceTags.getKey() != null && iotDeviceTags.getName() != null && iotDeviceTags.getValue() != null) {
                iotDeviceTags.setUpdateTime(new Date());
                iotDeviceTagsMapper.updateIotDeviceTags(iotDeviceTags);
            }
        }
        return 1;
    }
}
